package com.github.aloxc.plugin.restplus.i18n;

import com.github.aloxc.plugin.restplus.utils.StringUtils;
import com.intellij.ide.util.PropertiesComponent;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;
/**
 *
 * @author leero
 */
public final class I18Config {

    public static final Locale[] ALLOW_LANGUAGE = new Locale[]{
            Locale.CHINESE,
            Locale.JAPANESE,
            Locale.ENGLISH
    };
    public I18Config() {
    }


    @NotNull
    public final String getLocale() {
        PropertiesComponent instance = PropertiesComponent.getInstance();
        String locale = instance.getValue(I18Config.class.getName());
        if (StringUtils.isNullOrEmpty(locale)) {
            for (int i = 0; i < ALLOW_LANGUAGE.length; i++) {
                String lang = Locale.getDefault().getLanguage();
                if (areEqual(lang, ALLOW_LANGUAGE[i].getLanguage())) {
                    return ALLOW_LANGUAGE[i].getLanguage();
                }
            }
        }
        return locale;
    }


    public final void toggleLanguage(String locale) {
        PropertiesComponent instance = PropertiesComponent.getInstance();
        instance.setValue(I18Config.class.getName(), locale);

    }
    public static boolean areEqual(Object first, Object second) {
        return first == null ? second == null : first.equals(second);
    }
}